﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Windows.Forms;
using ELearningSystem.Core.Manager;
using ELearningSystem.Core.Manager.ExtensionMethod;
using ELearningSystem.DAO.DAO;
using ELearningSystem.Gateway.Entities;
using ELearningSystem.Gateway.Gateway;
using NHibernate;

namespace ELearningSystem.CreateDB
{
    public partial class ELearningSystemUI : Form
    {
        private ISessionFactory _sessionFactory;
        CommonGateway commonGateway = new CommonGateway();
        public ELearningSystemUI()
        {
            InitializeComponent();
        }

        private void createDbButton_Click(object sender, EventArgs e)
        {
            _sessionFactory = DatabaseConfigure.BuildSessionFactory();
            TreeContent treeContent = new TreeContent();
            treeContent.Text = "OOP";
            treeContent.ParentNode = null;
            treeContent.Position = 0;
            treeContent.Content = null;
            commonGateway.SaveOrUpdate(treeContent);
            MessageBox.Show("Database has been created");
        }

        private void insertButton_Click(object sender, EventArgs e)
        {
            _sessionFactory = DatabaseConfigure.BuildSessionFactory();
            TreeContent root = new TreeContent();
            root.Text = "OOP";
            root.ParentNode = null;
            root.Position = 0;
            root.Content = null;
            
            TreeContent whatIsObject = new TreeContent();
            whatIsObject.Text = "WHAT IS OBJECT";
            whatIsObject.Position = 1;
            whatIsObject.ParentNode = root;
            commonGateway.SaveOrUpdate(whatIsObject);

            TreeContent video1Content = new TreeContent();
            video1Content.ParentNode = whatIsObject;
            video1Content.Text = "Video";
            video1Content.Position = 1;
            video1Content.Content = new Content();
            video1Content.Content.Video = new VideoContent();
            video1Content.Content.IsVideo = true;
            video1Content.Content.Video.Link = "http://www.youtube.com/embed/OQjPcS1tkDQ";
            video1Content.Content.Video.Descriptions = new List<Description>() { new Description() { Text = "Video On What Is Object" } };
            video1Content.Content.Video.Text = "Video";
            commonGateway.SaveOrUpdate(video1Content);

            TreeContent practice1Content = new TreeContent();
            practice1Content.Text = "Practice";
            Practice practice = new Practice();
            practice.Question = "Write is the major difference between run-time error and compile-time error.";
            practice.Answers = new List<Answer>(){new Answer()
                                                      {
                                                          Text = "Run-time error is better than compile-time error.",
                                                          IsCorrect = false
                                                      },
                                                   new Answer()
                                                   {
                                                       Text = "Run-time error helps to discover the error in early stage of code-writing than compile-time error.",
                                                       IsCorrect = false
                                                    },
                                                    new Answer()
                                                      {
                                                          Text = "Compile-time error helps to discover the error in early stage of code-writing than run-time error.",
                                                          IsCorrect = true
                                                      },
                                                    new Answer()
                                                      {
                                                          Text = "None of the above",
                                                          IsCorrect = false
                                                      }};
            practice1Content.Content = new Content();
            practice1Content.Content.Practice = new Practice();
            practice1Content.Content.Practice = practice;
            practice1Content.ParentNode = whatIsObject;
            practice1Content.Position = 2;
            commonGateway.SaveOrUpdate(practice1Content);

            TreeContent practice2Content = new TreeContent();
            practice2Content.Text = "Practice";
            practice = new Practice();
            practice.Question = "Select two of the following answers applicable for List type ";
            practice.Answers = new List<Answer>(){new Answer()
                                                      {
                                                          Text = "We can tell the data type of it during declaration",
                                                          IsCorrect = true
                                                      },
                                                   new Answer()
                                                   {
                                                       Text = "Fixed size",
                                                       IsCorrect = false
                                                    },
                                                    new Answer()
                                                      {
                                                          Text ="Grows if it is required",
                                                          IsCorrect = true
                                                      },
                                                    new Answer()
                                                      {
                                                          Text = "Can’t deal with object type",
                                                          IsCorrect = false
                                                      }};
            practice2Content.Content = new Content();
            practice2Content.Content.Practice = new Practice();
            practice2Content.Content.Practice = practice;
            practice2Content.ParentNode = whatIsObject;
            practice2Content.Position = 3;
            commonGateway.SaveOrUpdate(practice2Content);

            TreeContent whatIsClass = new TreeContent();
            whatIsClass.Text = "WHAT IS CLASS";
            whatIsClass.Position = 2;
            whatIsClass.ParentNode = root;
            commonGateway.SaveOrUpdate(whatIsClass);

            video1Content = new TreeContent();
            video1Content.ParentNode = whatIsClass;
            video1Content.Text = "Video on Class";
            video1Content.Position = 1;
            video1Content.Content = new Content();
            video1Content.Content.Video = new VideoContent();
            video1Content.Content.IsVideo = true;
            video1Content.Content.Video.Link = "http://www.youtube.com/embed/ANTE74P5j_w";
            video1Content.Content.Video.Descriptions = new List<Description>() { new Description() { Text = "Video On What Is Class" } };
            video1Content.Content.Video.Text = "Video on class";
            commonGateway.SaveOrUpdate(video1Content);

            TreeContent practiceContent = new TreeContent();
            practiceContent.Text = "Practice class";
            practiceContent.ParentNode = whatIsClass;
            practiceContent.Position = 2;
            practice = new Practice();
            practice.Question = "When does a subclass override a method of its base class?";
            practice.Answers = new List<Answer>(){new Answer()
                                                      {
                                                          Text = "If base class has no such kind of functionality and sub class wants to introduce one.",
                                                          IsCorrect = false
                                                      },
                                                   new Answer()
                                                   {
                                                       Text = "Base class may contain partial functionality and sub class has some additional logic over it",
                                                       IsCorrect = true
                                                    },
                                                    new Answer()
                                                      {
                                                          Text = "For some security purpose sub class needs to override it",
                                                          IsCorrect = false
                                                      },
                                                    new Answer()
                                                      {
                                                          Text = "None of the above",
                                                          IsCorrect = false
                                                      }};
            practiceContent.Content = new Content();
            practiceContent.Content.Practice = new Practice();
            practiceContent.Content.Practice = practice;
            commonGateway.SaveOrUpdate(practiceContent);






            TreeContent associationRelationship = new TreeContent();
            associationRelationship.Text = "ASSOCIATION RELATIONSHIP";
            associationRelationship.Position = 3;
            associationRelationship.ParentNode = root;
            commonGateway.SaveOrUpdate(associationRelationship);

            video1Content = new TreeContent();
            video1Content.ParentNode = associationRelationship;
            video1Content.Text = "First Video";
            video1Content.Position = 1;
            video1Content.Content = new Content();
            video1Content.Content.Video = new VideoContent();
            video1Content.Content.IsVideo = true;
            video1Content.Content.Video.Link = "http://www.youtube.com/embed/OQjPcS1tkDQ"; ////
            video1Content.Content.Video.Descriptions = new List<Description>() { new Description() { Text = "First video On assosciation relationship" } };
            video1Content.Content.Video.Text = "First Video";
            commonGateway.SaveOrUpdate(video1Content);

            practice1Content = new TreeContent();
            practice1Content.Text = "First Practice";
            practice = new Practice();
            practice.Question = "An abstract class may have non-abstract method.";
            practice.Answers = new List<Answer>(){new Answer()
                                                      {
                                                          Text = "True",
                                                          IsCorrect = true
                                                      },
                                                   new Answer()
                                                   {
                                                       Text = "False",
                                                       IsCorrect = false
                                                    },
                                                    new Answer()
                                                      {
                                                          Text = "Not Applicable",
                                                          IsCorrect = false
                                                      }};
            practice1Content.Content = new Content();
            practice1Content.Content.Practice = new Practice();
            practice1Content.Content.Practice = practice;
            practice1Content.ParentNode = associationRelationship;
            practice1Content.Position = 2;
            commonGateway.SaveOrUpdate(practice1Content);

            practice2Content = new TreeContent();
            practice2Content.Text = "Second Practice";
            practice = new Practice();
            practice.Question = "A non-abstract class may have some abstract method ";
            practice.Answers = new List<Answer>(){new Answer()
                                                      {
                                                          Text = "True",
                                                          IsCorrect = false
                                                      },
                                                   new Answer()
                                                   {
                                                       Text = "False",
                                                       IsCorrect = true
                                                    },
                                                   };
            practice2Content.Content = new Content();
            practice2Content.Content.Practice = new Practice();
            practice2Content.Content.Practice = practice;
            practice2Content.ParentNode = associationRelationship;
            practice2Content.Position = 3;
            commonGateway.SaveOrUpdate(practice2Content);

            TreeContent video2Content = new TreeContent();
            video2Content.ParentNode = associationRelationship;
            video2Content.Text = "Second Video";
            video2Content.Position = 4;
            video2Content.Content = new Content();
            video2Content.Content.Video = new VideoContent();
            video2Content.Content.IsVideo = true;
            video2Content.Content.Video.Link = "http://www.youtube.com/embed/OQjPcS1tkDQ";
            video2Content.Content.Video.Descriptions = new List<Description>() { new Description() { Text = "Second video On assosciation relationship" } };
            video2Content.Content.Video.Text = "Second Video";
            commonGateway.SaveOrUpdate(video2Content);

            TreeContent inheritance = new TreeContent();
            inheritance.Text = "INHERITANCE";
            inheritance.Position = 2;
            inheritance.ParentNode = root;
            commonGateway.SaveOrUpdate(inheritance);

            video1Content = new TreeContent();
            video1Content.ParentNode = inheritance;
            video1Content.Text = "Video";
            video1Content.Position = 1;
            video1Content.Content = new Content();
            video1Content.Content.Video = new VideoContent();
            video1Content.Content.IsVideo = true;
            video1Content.Content.Video.Link = "http://www.youtube.com/embed/o8vmy1NZkfM"; ////
            video1Content.Content.Video.Descriptions = new List<Description>() { new Description() { Text = "First video On Inheritance" } };
            video1Content.Content.Video.Text = "First Video";
            commonGateway.SaveOrUpdate(video1Content);
            UserManager userManager =new UserManager();

            Learner adminLearner = new Learner();
            adminLearner.Name = "admin";
            adminLearner.EmailAddress = "admin@gmail.com";
            adminLearner.Password = "admin123";
            adminLearner.Role = "Admin";
            commonGateway.SaveOrUpdate(adminLearner);
            //if(!Membership.ValidateUser(adminLearner.Name,adminLearner.Password))
            //{
            //    MembershipUser aMUser = Membership.CreateUser(adminLearner.Name, adminLearner.Password, adminLearner.EmailAddress);
            //    aMUser.IsApproved = true;
            //    string[] users = new string[1];
            //    users[0] = aMUser.UserName;
            //    Roles.AddUsersToRole(users, "Admin");
            //}
           


            string message;
            LearnerDAO firstLearner = new LearnerDAO();
            firstLearner.Name = "shoaib";
            firstLearner.EmailAddress = "shoaib@gmail.com";
            firstLearner.Password = "shoaib123";
            firstLearner.Role = "Learner";
            userManager.SaveOrUpdateLearner(firstLearner, out message);

            LearnerDAO secondLearner= new LearnerDAO();
            secondLearner.Name = "mamun";
            secondLearner.EmailAddress = "mamun@gmail.com";
            secondLearner.Password = "mamun123";
            secondLearner.Role = "Learner";
            userManager.SaveOrUpdateLearner(secondLearner, out message);
            MessageBox.Show("Default values has been inserted");
        }
    }
}
